//
//  LogInViewController.m
//  xmppones
//
//  Created by 李立达 on 2017/2/23.
//  Copyright © 2017年 李立达. All rights reserved.
//

#import "LogInViewController.h"
#import "BSTextFiledView.h"
#import "xmppmannger.h"
@interface LogInViewController ()
@property (nonatomic,strong)BSTextFiledView *usernameTextFiled;
@property (nonatomic,strong)BSTextFiledView *passwordTestFiled;
@property (nonatomic,strong)BSTextFiledView *hostName;
@property (nonatomic,strong)BSTextFiledView *hostPort;
@property (nonatomic,strong)UIButton *loginButton;
@end

@implementation LogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUi];
}

-(void)createUi
{
    self.view.backgroundColor = COLOR_CONTROLLER_BACKGROUND;
    self.usernameTextFiled = [[BSTextFiledView alloc]initWithFrame:CGRectMake(40, 100, KDeviceWith-80, 50)];
    self.usernameTextFiled.backgroundColor = COLOR_BLUDBUTTON;
    self.usernameTextFiled.placeholder = @"username";
    [self.view addSubview:self.usernameTextFiled];
    self.passwordTestFiled = [[BSTextFiledView alloc]initWithFrame:CGRectMake(40, self.usernameTextFiled.bottom+30, KDeviceWith-80, 50)];
    self.passwordTestFiled.placeholder = @"password";
    self.passwordTestFiled.backgroundColor = COLOR_BLUDBUTTON;
    [self.view addSubview:self.passwordTestFiled];
    
    //    self.hostName = [[BSTextFiledView alloc]initWithFrame:CGRectMake(40, self.passwordTestFiled.bottom+30, (KDeviceWith - 80 - 20)/3*2, 40)];
    //    self.hostName.placeholder = @"hostName";
    //    [self.view addSubview:self.hostName];
    //    self.hostPort = [[BSTextFiledView alloc]initWithFrame:CGRectMake(self.hostName.right+20, self.passwordTestFiled.bottom+30, (KDeviceWith - 80 - 20)/3, 40)];
    //    self.hostPort.placeholder = @"hostPort";
    //    [self.view addSubview:self.hostPort];
    
    self.loginButton = [[UIButton alloc]initWithFrame:CGRectMake(40, self.passwordTestFiled.bottom+40, KDeviceWith-80, 50)];
    self.loginButton.backgroundColor = COLOR_BLUDBUTTON;
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    
}

-(void)loginClick:(UIButton*)sender
{
    BOOL islogin = self.usernameTextFiled.text.length>0 && self.passwordTestFiled.text.length>0 ;
    
    if(islogin)
    {
        [getXMPPManager() loginWithName:self.usernameTextFiled.text andPassword:self.passwordTestFiled.text sucessLogin:^{
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:self.usernameTextFiled.text forKey:USERNAME];
            [defaults setObject:self.passwordTestFiled.text forKey:PASSWORD];
            [getAppdelegate() isjumpBarController:YES];
        }];
    }
}


@end
